package cn.wlbw.controller;

import act.controller.Controller;
import cn.wlbw.dao.NDao;
import cn.wlbw.model.SysPermission;
import cn.wlbw.utils.LayuiPage;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("sysPermission")
public class PermissionController extends Global {

    @GetAction("/pages/member/permission")
    public void permission() {
    }

    @GetAction("permissionAddView")
    public void permission_add() {
    }

    private Dao dao = NDao.dao;


    @PostAction("addPermission")
    public Map<String, Object> addPermission(SysPermission permission) {
        dao.insert(permission);
        return LayuiPage.resultData(1000,"操作成功");
    }

    @GetAction("permissionData")
    public Map<String, Object> permissionData() {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<SysPermission> permissionList = dao.query(SysPermission.class, Cnd.NEW());
        Map map = null;
        for (SysPermission permission : permissionList) {
            map = permissionDataMap(map, permission);
            mapList.add(map);
        }
        return LayuiPage.resultData(0, "获取数据", mapList.size(), mapList);
    }


    /**
     * 下拉菜单树
     *
     * @return
     */
    @GetAction("selectOptionTree")
    public Object selectOptionTree() {
        List<SysPermission> permissionList = dao.query(SysPermission.class, Cnd.NEW());
        List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
        for (SysPermission permission : permissionList) {
            Map<String, Object> map = null;
            if (permission.getParentId() == 0) {
                map = selectOptionMap(map, permission);
            }
            if (map != null) {
                treeList.add(map);
            }
        }
        return treeList;
    }

    /**
     * 递归设置下拉权限树
     *
     * @param parentId
     * @return
     */
    public List<Map<String, Object>> createOpionChildren(int parentId) {
        List<SysPermission> childrenList = dao.query(SysPermission.class, Cnd.where("parent_id", "=", parentId));
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
        for (SysPermission permission : childrenList) {
            Map<String, Object> map = null;
            if (permission.getParentId() == parentId) {
                map = selectOptionMap(map, permission);
            }
            if (map != null) {
                childList.add(map);
            }
        }
        return childList;
    }

    /**
     * 下拉菜单树map集合数据
     *
     * @param map
     * @param permission
     * @return
     */
    public Map selectOptionMap(Map<String, Object> map, SysPermission permission) {
        if (permission.getType() != 1) { //资源类型不为按钮
            map = new HashMap<String, Object>();
            map.put("id", permission.getId());
            map.put("name", permission.getName());
            if (permission.getParentId() == 0) {
                map.put("open", true);
            }//展开
            map.put("children", createOpionChildren(permission.getId()));
        }
        return map;
    }

    /**
     * 权限列表map数据集合
     *
     * @param map
     * @param permission
     * @return
     */
    public Map permissionDataMap(Map<String, Object> map, SysPermission permission) {
        map = new HashMap<String, Object>();
        map.put("id", permission.getId());
        map.put("pid", permission.getParentId());
        map.put("name", permission.getName());
        map.put("type", permission.getType());
        map.put("url", permission.getUrl());
        map.put("status", permission.getStatus());
        map.put("priority", permission.getPriority());
        map.put("identification", permission.getIdentification());
        map.put("icon", permission.getIcon());
        return map;
    }
}
